package com.finance.system.accountbook.controller;


import com.finance.system.accountbook.entity.CashRecordEntity;
import com.finance.system.accountbook.service.ICashRecordService;
import com.finance.system.support.bean.OffsetQuery;
import com.finance.system.support.bean.OffsetResult;
import com.finance.system.support.bean.ResultBean;
import com.finance.system.support.bean.ResultConstant;
import com.finance.system.accountbook.entity.CashRecordInbound;
import com.finance.system.user.entity.UserInfo;
import com.finance.system.accountbook.entity.CashRecordOutbound;
import com.finance.system.configure.websecurity.AuthUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.finance.system.user.controller.BaseController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * <p>
 * 消费记录表，存放每笔消费的信息 前端控制器
 * </p>
 *
 * @author whdong
 * @since 2019-12-24
 */
@Api(value = "账本消费模块",tags = {"消费记录"})
@RestController
@RequestMapping("/account_book/cash")
public class CashRecordController extends BaseController {

    @Autowired
    private ICashRecordService cashRecordService;


    @PostMapping("getPageDataGroup")
    @ApiOperation(value = "查询交易记录的分页数据", notes = "查询交易记录")
    public OffsetResult<CashRecordOutbound> getPageDataGroup(
            @ApiIgnore @AuthUser UserInfo userInfo,
            @RequestBody CashRecordInbound cashRecord,
            @RequestBody OffsetQuery<CashRecordEntity> offsetQuery){
        System.out.println(userInfo);
        return cashRecordService.getPageDataByGroup(cashRecord,offsetQuery);
    }

    @PostMapping("getGroupSpendingStatistics")
    @ApiOperation(value = "获取某个组的账本的消费统计数据", notes = "组消费统计")
    public ResultBean<Map<String, Object>> getGroupSpendingStatistics(@RequestBody CashRecordInbound cashRecord,
                                                                      @RequestBody OffsetQuery<CashRecordEntity> offsetQuery){
        return cashRecordService.getSpendingStatisticsByGroup(cashRecord,offsetQuery);
    }

    @PostMapping("calcGroupRecord")
    @ApiOperation(value = "结算某个组的帐", notes = "组消费结算")
    public ResultBean calcGroupRecord(@RequestBody CashRecordInbound cashRecord){
        return cashRecordService.closeRecordOfSpendingStatisticsByGroup(cashRecord);
    }

    @PostMapping("addOne")
    @ApiOperation(value = "新增交易记录", notes = "新增交易记录")
    public ResultBean<CashRecordEntity> addOne(@RequestBody  CashRecordInbound cashRecord){
        return cashRecordService.addOneRecord(cashRecord);

    }

    @PostMapping("modifyById")
    @ApiOperation(value = "更新交易记录", notes = "更新交易记录")
    public ResultBean<CashRecordEntity> modifyById(@RequestBody  CashRecordInbound cashRecord){
        return cashRecordService.modifyById(cashRecord);
    }

    @PutMapping("getRecordDetail")
    @ApiOperation(value = "获取交易记录详情", notes = "记录详情")
    public ResultBean<CashRecordOutbound> getRecordDetail(
            @ApiIgnore @AuthUser UserInfo userInfo,
            @RequestBody Map<String,Object> paraMap){
        System.out.println(userInfo);
        return cashRecordService.getRecordById((String)paraMap.get("id"));
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除交易记录", notes = "删除交易")
    public ResultBean delete(String id){
        cashRecordService.removeById(id);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.DELETE_SUCCESS_MESSAGE,null);
    }
}
